package com.mapreduce;

import com.bean.AvgSumTemp;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
import java.net.URI;

public class AvgYearTempMR extends Configured implements Tool {
    static class AvgYearMRTempMapper
            extends Mapper<LongWritable, Text,Text,AvgSumTemp>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws
                IOException, InterruptedException {
            String[] val=value.toString().split(",");
            AvgSumTemp as=new AvgSumTemp(1,Double.parseDouble(val[1]));
            context.write(new Text(val[0]),as);
        }
    }
    static class AvgYearTempMRCombiner
            extends Reducer<Text,AvgSumTemp,Text,AvgSumTemp>{
        @Override
        protected void reduce(Text key, Iterable<AvgSumTemp> values, Context
                context) throws IOException, InterruptedException {
            int nums=0;
            double sum_temp=0;
            for(AvgSumTemp as:values){
                nums+=as.getNum();
                sum_temp+=as.getSum_temp();
            }
            context.write(key,new AvgSumTemp(nums,sum_temp));
        }
    }
    static class AvgYearMRTempReduce
            extends Reducer<Text,AvgSumTemp,Text, DoubleWritable>{
        @Override
        protected void reduce(Text key, Iterable<AvgSumTemp> values, Context
                context) throws IOException, InterruptedException {
            double sum_temp=0;
            int nums=0;
            for(AvgSumTemp as:values){
                nums=as.getNum();
                sum_temp=as.getSum_temp();
            }
            context.write(key,new DoubleWritable(sum_temp/nums));
        }
    }
    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();

        // 指定输入输出路径
        Path input = new Path(
                "hdfs://192.168.10.11:9000/sortdata");
        Path output = new Path(
                "hdfs://192.168.10.11:9000/test");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                ,conf);
        if (fs.exists(output)) fs.delete(output,true);


        Job job = Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("Average Temperature By Year");
        job.setMapperClass(AvgYearMRTempMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(AvgSumTemp.class);
        job.setCombinerClass(AvgYearTempMRCombiner.class);
        job.setReducerClass(AvgYearMRTempReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        TextInputFormat.addInputPath(job, input);
        TextOutputFormat.setOutputPath(job, output);
        return (job.waitForCompletion(true) ? 0 : 1);
    }
    public static void main(String[] args) throws Exception {
        System.exit(
                new ToolRunner().run(
                        new AvgYearTempMR(),args));
    }
}
